package org.dialMsg.Server.ServerImp;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.mjoys.report.common.pojo.mq.body.FactAiCallMessage;
import com.mjoys.report.starter.api.FactMessageSendApi;
import org.apache.commons.lang3.RandomUtils;
import org.dialMsg.Pojo.TDialResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;


/**
 * @author DPF
 */
@Service
public class FactMessageSendApiServerImp {
    @Autowired
    private FactMessageSendApi factMessageSendApi;
    private static final Logger logger = LoggerFactory.getLogger(FactMessageSendApiServerImp.class);

    public void sendFactMessage(List<TDialResult> tDialResults) {

        for (TDialResult tDialResult : tDialResults) {
            try {
                FactAiCallMessage factAiCallMessage = createFactAiCallMessage(tDialResult);
                if (tDialResult.getCallType() == 1) {
                    setCallTypeOneSpecificFields(factAiCallMessage, tDialResult);
                } else {
                    setOtherCallTypeSpecificFields(factAiCallMessage, tDialResult);
                }
                factMessageSendApi.send(factAiCallMessage);
            } catch (Exception e) {
                logger.error("调用 insertDimDataBatch(tDialResults) 失败，忽略异常继续执行，error={}", e.getMessage(), e);
            }
        }
    }

    private FactAiCallMessage createFactAiCallMessage(TDialResult tDialResult) {
        FactAiCallMessage factAiCallMessage = new FactAiCallMessage();
        factAiCallMessage.setAiCallLogId(tDialResult.getId());
        factAiCallMessage.setNumberType(2);
        factAiCallMessage.setExecuteStatus(1);
        factAiCallMessage.setBatchId(Long.valueOf(tDialResult.getDataSourceBatchId()));
        factAiCallMessage.setBatchDetailId(tDialResult.getDataSourceBatchDetailId());
        factAiCallMessage.setDataOrgCode(tDialResult.getBankOrg());
        factAiCallMessage.setTenantId(tDialResult.getAccountId());
        factAiCallMessage.setSceneId(tDialResult.getBusinessId());
        factAiCallMessage.setLineNumber(tDialResult.getOutboundNumber());
        factAiCallMessage.setEquipmentId(1);
        factAiCallMessage.setTargetPhone(tDialResult.getPhone());
        factAiCallMessage.setDistributeFlag(1);
        factAiCallMessage.setKeyboard("#1");
        factAiCallMessage.setDialogTypes(tDialResult.getCallText());
        factAiCallMessage.setTargetProCity(tDialResult.getProCity());
        factAiCallMessage.setTargetCarrier(tDialResult.getCarried());
        factAiCallMessage.setCallTime(LocalDateTimeUtil.of(tDialResult.getDailTime()));
        factAiCallMessage.setCustomerName(tDialResult.getBankManager());
        factAiCallMessage.setCallCount(tDialResult.getCallCount());
        factAiCallMessage.setDurationOfRinging(tDialResult.getDurationOfRinging());
        factAiCallMessage.setCallStatus(tDialResult.getCallStatus());
        factAiCallMessage.setSeatId(tDialResult.getFollowUser());
        factAiCallMessage.setLatestSeatId(tDialResult.getFollowUser());
        factAiCallMessage.setCallType(tDialResult.getCallType());
        factAiCallMessage.setDialogTotalTime(tDialResult.getCallTime() + tDialResult.getDurationOfRinging() + tDialResult.getDurationOfSeatConversing());
        factAiCallMessage.setDurationOfAiConversing(tDialResult.getDurationOfAiConversing());
        factAiCallMessage.setFeeCallTime(tDialResult.getCallTime());
        factAiCallMessage.setRoundCount(tDialResult.getRoundCount());
        factAiCallMessage.setUserClassIdAi(tDialResult.getResultStatusDesc());
        factAiCallMessage.setCallRecordPath(tDialResult.getCallRecordPath());
        factAiCallMessage.setTransferWaitTime(tDialResult.getInvitationCount());
        return factAiCallMessage;
    }

    private void setCallTypeOneSpecificFields(FactAiCallMessage factAiCallMessage, TDialResult tDialResult) {
        // 只有 callType 为 1 时需要设置的字段
        factAiCallMessage.setInterOperatorId(null);
        factAiCallMessage.setTransferSeatId(null);
        factAiCallMessage.setAllowInterSeatId(null);
    }

    private void setOtherCallTypeSpecificFields(FactAiCallMessage factAiCallMessage, TDialResult tDialResult) {
        if (tDialResult.getCallType() == 2) {
            factAiCallMessage.setTransferSeatId(tDialResult.getFollowUser());
            factAiCallMessage.setTransferStatus(tDialResult.getBridgeResult());
            factAiCallMessage.setDurationSeatRing(tDialResult.getDurationOfSeatRing());
            factAiCallMessage.setDurationSeatConverse(tDialResult.getDurationOfSeatConversing());
            factAiCallMessage.setTransferWaitTime(tDialResult.getDurationOfSeatRing());
            if ((tDialResult.getBridgeResult() == 0 || tDialResult.getBridgeResult() == 1 || tDialResult.getBridgeResult() == 5)){
                factAiCallMessage.setTransferSeatId(null);
            }

        }
        // 处理主动介入
        else if (tDialResult.getCallType() == 3) {
            factAiCallMessage.setInterOperatorId(tDialResult.getFollowUser());
            factAiCallMessage.setAllowInterSeatId(tDialResult.getFollowUser());
            factAiCallMessage.setActiveInterventionStatus(tDialResult.getBridgeResult());
            factAiCallMessage.setDurationSeatRing(tDialResult.getDurationOfSeatRing());
            factAiCallMessage.setDurationSeatConverse(tDialResult.getDurationOfSeatConversing());
            factAiCallMessage.setTransferWaitTime(tDialResult.getDurationOfSeatRing());
        }
    }
}